<script setup>
import { ref, computed } from 'vue';
import router from '@/router/index.js';

// 模拟数据
const notices = ref(Array.from({length: 35}, (_, i) => ({
  id: i + 1,
  content: `西南石油大学2024年考核招聘本科教学实验教师公告（${i + 1}）`,
  date: `2024-04-${String(26 + i).padStart(2, '0')}`
})))

// 分页配置
const pageSize = 10;
const total = computed(() => notices.value.length);
const currentPage = ref(1);

// 当前显示内容
const currentNotices = computed(() => {
  const start = (currentPage.value - 1) * pageSize;
  return notices.value.slice(start, start + pageSize);
});

// 分页切换
const handlePageChange = (val) => {
  currentPage.value = val;
};

// 分页显示文字
const paginationText = computed(() => {
  const start = (currentPage.value - 1) * pageSize + 1;
  const end = Math.min(currentPage.value * pageSize, total.value);
  return `显示第 ${start} 至 ${end} 项结果，共 ${total.value} 项`;
});

function navigateToDetail() {
  router.push('/index/noticeDetail');
}
</script>

<template>
  <div class="notice-container">
    <!-- 标题栏 -->
    <div class="title-bar">
      <div class="blue-line"></div>
      <h2>通知公告</h2>
    </div>

    <!-- 内容区域 -->
    <div class="content-box">
      <div v-for="(item, index) in currentNotices" :key="item.id" class="notice-item">
        <div class="item-content" @click="navigateToDetail()">
          <span class="text">{{ item.content }}</span>
          <span class="time">发布时间 : {{ item.date }}</span>
        </div>
        <div v-if="index !== currentNotices.length - 1" class="divider"></div>
      </div>
    </div>

    <!-- 分页区域 -->
    <div class="pagination-container">
      <el-pagination background layout="prev, pager, next" :page-size="pageSize" :total="total" @current-change="handlePageChange"/>
      <div class="pagination-text">
        {{ paginationText }}
      </div>
    </div>
  </div>
</template>

<style scoped>
.notice-container {
  width: 80%;
  margin: 0 10%;
  padding: 30px 0;
}

.title-bar {
  display: flex;
  align-items: center;
  width: 100%;
  margin-bottom: 20px;
}

.blue-line {
  width: 4px;
  height: 24px;
  background: #3b82f6;
  margin-right: 12px;
}

.title-bar h2 {
  font-size: 20px;
  color: #1e3a8a;
  margin: 0;
}

.content-box {
  background: white;
  width: 100%;
  border-radius: 6px;
  box-shadow: 0 2px 8px rgba(0,0,0,0.1);
  margin-bottom: 20px;
}

.notice-item {
  padding: 16px 20px;
}

.item-content {
  display: flex;
  justify-content: space-between;
  align-items: center;
  cursor: pointer;
}

.text {
  flex: 1;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  color: #334155;
}

.time {
  flex-shrink: 0;
  margin-left: 30px;
  color: #64748b;
  font-size: 14px;
}

.divider {
  height: 1px;
  background: #e2e8f0;
  width: 80%;
  margin: 0 auto;
}

.pagination-container {
  display: flex;
  justify-content: space-between;
  align-items: center;
  width: 100%;
}

.pagination-text {
  color: #64748b;
  font-size: 14px;
}

/* 分页样式调整 */
:deep(.el-pagination) {
  padding: 0;
  justify-content: flex-start;
}

:deep(.el-pagination.is-background .btn-prev),
:deep(.el-pagination.is-background .btn-next),
:deep(.el-pagination.is-background .el-pager li) {
  background: #fff;
  border: 1px solid #e2e8f0;
  border-radius: 4px;
  margin: 0 4px;
}

:deep(.el-pagination.is-background .el-pager li:not(.is-disabled).is-active) {
  background: #3b82f6;
  color: white;
}
</style>